package com.renjun.smweb.controller.RegisterController;

import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.annotation.AuthorityDeclaration;
import com.renjun.smweb.commons.util.ResponseUtils;
import com.renjun.smweb.dao.pojo.LoginInfo;
import com.renjun.smweb.exceptions.BaseException;
import com.renjun.smweb.exceptions.DictIsNotFoundException;
import com.renjun.smweb.exceptions.FileTypeNotFoundException;
import com.renjun.smweb.service.RegisterService.RegisterService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by huamin on 2017/2/21.
 * 报名登记M6
 */
@Controller
@RequestMapping("/register")
public class RegisterController {
    @Resource
    private RegisterService registerService;


    /**
     * 新生登记列表查询
     * @param request
     * @return []
     */
    @RequestMapping(value = "/queryRegisterList", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M6-1", desc = "新生登记列表查询",reqParam = "",resBody = "",errMsgs = "sql异常")
    public JSONObject queryRegisterList(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        // TODO: 新生登记列表查询需要联调 2017年2月22日10:39:50
        try {
            rs = registerService.queryRegisterList(params);
        } catch (SQLException  e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 查看单个登记信息
     * @param request
     * @return []
     */
    @RequestMapping(value = "/registerDetail", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M6-1-1", desc = "查看单个登记信息",reqParam = "",resBody = "",errMsgs = "sql异常")
    public JSONObject registerDetail(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        // TODO: 查看单个登记信息 2017年2月22日10:39:50
        try {
            rs = registerService.registerDetail(params);
        } catch (SQLException  e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 新生登记 金山
     * @param request
     * @return []
     */
    @RequestMapping(value = "/addRegister", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M6-2", desc = "新生登记",reqParam = "",resBody = "",errMsgs = "sql异常")
    public JSONObject addRegister(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        //新生登记需要联调 2017年2月22日10:39:50
        try {
            rs = registerService.addRegister(params);
        } catch (SQLException|BaseException|ParseException  e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }


    /**
     * 新生登记-虹口
     * @param request
     * @return []
     */
    @RequestMapping(value = "/addRegisterHk", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M6-2-hk", desc = "新生登记-虹口",reqParam = "",resBody = "",errMsgs = "sql异常")
    public JSONObject addRegisterHk(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        // TODO: 新生登记需要联调 2017年4月12日13:01:30
        try {
            rs = registerService.addRegisterHk(params);
        } catch (SQLException|BaseException|ParseException   e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 新生登记修改
     * @param request
     * @return []
     */
    @RequestMapping(value = "/modifyRegister", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M6-3", desc = "新生登记修改",reqParam = "",resBody = "",errMsgs = "sql异常")
    public JSONObject modifyRegister(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        // TODO: 新生登记修改需要联调 2017年2月22日10:39:50
        try {
            rs = registerService.modifyRegister(params);
        } catch (SQLException|BaseException|ParseException   e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 新生登记删除
     * @param request
     * @return []
     */
    @RequestMapping(value = "/removeRegister", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M6-4", desc = "新生登记删除",reqParam = "",resBody = "",errMsgs = "sql异常")
    public JSONObject removeRegister(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        // TODO: 新生登记删除需要联调 2017年2月22日10:39:50
        try {
            rs = registerService.removeRegister(params);
        } catch (SQLException|BaseException|ParseException  e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 批量导入删除
     * @param request
     * @return []
     */
    @RequestMapping(value = "/batchImportRemoveRegister", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M6-8", desc = "批量导入删除",reqParam = "",resBody = "",errMsgs = "sql异常")
    public JSONObject batchImportRemoveRegister(@RequestParam(value = "upfile_attachment", required = false) MultipartFile file,
                                                HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        LoginInfo loginInfo = (LoginInfo)  request.getSession().getAttribute("userinfo");
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        // TODO: 2017年2月22日 批量导入删除未完成，格式未定义下来 向立军
        try {
            params.put("file",file);
            params.put("userinfo",loginInfo);
            rs = registerService.batchImportRemoveRegister(params);
        } catch (Exception  e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }


    /**
     * 生成新生登记表
     * @param request
     * @return []
     */
    @RequestMapping(value = "/generateRegister", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M6-5", desc = "生成新生登记表",reqParam = "",resBody = "",errMsgs = "sql异常")
    public JSONObject generateRegister(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        // TODO: 2017年2月22日 生成新生登记表未完成，格式未定义下来 向立军
        try {
            rs = registerService.generateRegister(params);
        } catch (Exception  e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 批量导入
     * @param request
     * @return []
     */
    @RequestMapping(value = "/batchImportRegister", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M6-6", desc = "批量导入",reqParam = "",resBody = "",errMsgs = "sql异常")
    public JSONObject batchImportRegister(@RequestParam(value = "upfile_attachment", required = false) MultipartFile file,
                                      HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();

        LoginInfo loginInfo = (LoginInfo)  request.getSession().getAttribute("userinfo");
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        // TODO: 2017年2月22日 批量导入未完成，格式未定义下来 向立军
        try {
            params.put("file",file);
            params.put("userinfo",loginInfo);
            rs = registerService.batchImportRegister(params);
        } catch (Exception  e) {
            e.printStackTrace();
           return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 批量导出
     * @param request
     * @return []
     */
    @RequestMapping(value = "/exportRegister", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M6-7", desc = "批量导出",reqParam = "",resBody = "",errMsgs = "sql异常")
    public JSONObject exportRegister(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        // TODO: 2017年2月22日 批量导出需要联调测试 向立军
        try {
            rs = registerService.exportRegister(params);
        } catch (SQLException|FileTypeNotFoundException|IOException|DictIsNotFoundException  e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 批量生成入学信息登记表
     * @author lrs
     * @param request params:{条件参数}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @RequestMapping(value = "/batchGeneraterRegistrationForm", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M6-9", desc = "批量生成入学信息登记表")
    public JSONObject batchGeneraterRegistrationForm(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = registerService.batchGeneraterRegistrationForm(params);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 虹口新开接口-查询学生信息，全区匹配，然后进行登记进入baominginfo表
     * @author 向立军
     * @param request params:{条件参数}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @RequestMapping(value = "/queryRegisterDistrictCode", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M6-19", desc = "虹口查询全区登记学生",reqParam = "",resBody = "",errMsgs = "sql异常")
    public JSONObject queryRegisterDistrictCode(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = registerService.queryRegisterDistrictCode(params);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 登记学生，虹口区根据，证件号码，姓名，出生日期，匹配的学生进行登记
     * @author 向立军
     * @param request params:{条件参数}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @RequestMapping(value = "/addRegisterSchool", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M6-20", desc = "虹口登记学生",reqParam = "",resBody = "",errMsgs = "sql异常,本校登记不能重复登记")
    public JSONObject addRegisterSchool(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = registerService.addRegisterSchool(params);
        } catch (SQLException|BaseException|ParseException e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 取消报名
     * @author 向立军
     * @param request params:{条件参数}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @RequestMapping(value = "/cancelRegisterSchool", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M6-21", desc = "取消报名-虹口",reqParam = "",resBody = "",errMsgs = "sql异常,本校登记不能重复登记")
    public JSONObject cancelRegisterSchool(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = registerService.cancelRegisterSchool(params);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }


    /**
     * 根据用户信息，不同办学等级获取时间范围
     * @author 向立军
     * @param request params:{opentype:(1,2,3,4)}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @RequestMapping(value = "/getCheckTime", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M6-22", desc = "根据用户信息，不同办学等级获取时间范围",reqParam = "",resBody = "",errMsgs = "sql异常,本校登记不能重复登记")
    public JSONObject getCheckTime(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = registerService.getCheckTime(params);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

}
